﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using T.Settlement.API.Application.Command.PaymentCommand;
using T.Settlement.Repository;
using T.Settlement.Repository.SettlementInterface;

namespace T.Settlement.API.Application.CommandHandlers.PaymentHandler
{
    public class PaymentApprovalCreateCommandHandler : IRequestHandler<PaymentApprovalCreateCommand, int>
    {
        /// <summary>
        /// 审批仓储
        /// </summary>
        private readonly IApprovalRecorddSRepository _repository;
        /// <summary>
        /// 付款仓储
        /// </summary>
        private readonly IPaymentSRepository _paymentRepository;
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly SEFDbcontext _db;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="paymentRepository"></param>
        public PaymentApprovalCreateCommandHandler(IApprovalRecorddSRepository repository, IPaymentSRepository paymentRepository, SEFDbcontext db)
        {
            _repository = repository;
            _paymentRepository = paymentRepository;
            _db = db;
        }
        public async Task<int> Handle(PaymentApprovalCreateCommand request, CancellationToken cancellationToken)
        {
            var obj=(await _paymentRepository.GetAllAsync()).Where(x=>x.PaymentId==request.PaymentId).FirstOrDefault();
            if (request.ApprovalResult=="通过")
            {
                await _repository.AddAsync(request);
                obj.ApprovalStatus = 3;
                obj.Approver = request.Approver;
                await _paymentRepository.Update(obj);
            }
            else if (request.ApprovalResult == "通过并终止审批")
            {
                await _repository.AddAsync(request);
                obj.ApprovalStatus=3;
                obj.Approver = request.Approver;
                await _paymentRepository.Update(obj);
            }
            else if(request.ApprovalResult == "拒绝")
            {
                await _repository.AddAsync(request);
                obj.ApprovalStatus = 4;
                obj.Approver = request.Approver;
                await _paymentRepository.Update(obj);
            }
            else if( request.ApprovalResult == "通过并转批")
            {
                await _repository.AddAsync(request);
            }
            return 1;
        }
    }
}
